/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.system.service.impl;


import com.koron.common.domain.DataBean;
import com.koron.common.generator.DefaultIdGenerator;
import com.koron.system.domain.Announcement;
import com.koron.system.mapper.AnnouncementMapper;
import com.koron.system.service.api.AnnouncementService;
import com.koron.system.utils.BusinessCode;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.Date;
import java.util.List;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    private final Logger logger= LoggerFactory.getLogger(AnnouncementServiceImpl.class);
    @Override
    public MessageBean<Integer> save(Announcement announcement) {
        MessageBean<Integer> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(announcement.getTitle())){
                msg.setCode(BusinessCode.ANNOUNCEMENT_EMPTY_TITLE.getCode());
                msg.setDescription(BusinessCode.ANNOUNCEMENT_EMPTY_TITLE.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(announcement.getContent())){
                msg.setCode(BusinessCode.ANNOUNCEMENT_EMPTY_CONTENT.getCode());
                msg.setDescription(BusinessCode.ANNOUNCEMENT_EMPTY_CONTENT.getDescription());
                return msg;
            }
            if (null==announcement.getBeginTime()){
                msg.setCode(BusinessCode.ANNOUNCEMENT_EMPTY_BEGINTIME.getCode());
                msg.setDescription(BusinessCode.ANNOUNCEMENT_EMPTY_BEGINTIME.getDescription());
                return msg;
            }
            if (null==announcement.getEndTime()){
                msg.setCode(BusinessCode.ANNOUNCEMENT_EMPTY_ENDTIME.getCode());
                msg.setDescription(BusinessCode.ANNOUNCEMENT_EMPTY_ENDTIME.getDescription());
                return msg;
            }
            if (null==announcement.getAnnouIndex()){
                msg.setCode(BusinessCode.ANNOUNCEMENT_EMPTY_ANNOUINDEX.getCode());
                msg.setDescription(BusinessCode.ANNOUNCEMENT_EMPTY_ANNOUINDEX.getDescription());
                return msg;
            }
            Integer value=0;
            if (StringUtils.isEmpty(announcement.getId())){
                //新增
                announcement.setAddTime(announcement.getUpdateTime());
                announcement.setAddUser(announcement.getUpdateUser());
                announcement.setIsRemove(0);
                announcement.setId(DefaultIdGenerator.getInstance().generateLongId());
                value=factory.getMapper(AnnouncementMapper.class).insert(announcement);
            }else{
                //修改
                value=factory.getMapper(AnnouncementMapper.class).update(announcement);
            }
            if (value>0){
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription("保存成功");
                msg.setData(value);
                return msg;
            }else {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("保存失败");
                msg.setData(value);
                return msg;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<Integer> batchDeteleLogic(List<String> idList) {
        MessageBean<Integer> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            if (null==idList||idList.size()<=0){
                msg.setCode(BusinessCode.ANNOUNCEMENT_EMPTY_CHANGE_DATA.getCode());
                msg.setDescription(BusinessCode.ANNOUNCEMENT_EMPTY_CHANGE_DATA.getDescription());
                return msg;
            }
            Integer value=factory.getMapper(AnnouncementMapper.class).batchDeteleLogic(idList);
            if (value>0){
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription("删除成功");
                msg.setData(value);
                return msg;
            }else {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("删除失败");
                msg.setData(value);
                return msg;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<Integer> batchDetele(List<String> idList) {
        MessageBean<Integer> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            if (null==idList||idList.size()<=0){
                msg.setCode(BusinessCode.ANNOUNCEMENT_EMPTY_CHANGE_DATA.getCode());
                msg.setDescription(BusinessCode.ANNOUNCEMENT_EMPTY_CHANGE_DATA.getDescription());
                return msg;
            }
            Integer value=factory.getMapper(AnnouncementMapper.class).batchDetele(idList);
            if (value>0){
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription("删除成功");
                msg.setData(value);
                return msg;
            }else {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("删除失败");
                msg.setData(value);
                return msg;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<Announcement> queryOne(String id) {
        MessageBean<Announcement> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(id)){
                msg.setCode(BusinessCode.ANNOUNCEMENT_EMPTY_CHANGE_DATA.getCode());
                msg.setDescription(BusinessCode.ANNOUNCEMENT_EMPTY_CHANGE_DATA.getDescription());
                return msg;
            }
            Announcement announcement=factory.getMapper(AnnouncementMapper.class).queryOne(id);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription("获取公告详情成功");
            msg.setData(announcement);
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<DataBean<Announcement>> list(String title, Date beginTime, Date endTime, Integer start, Integer rows) {
        MessageBean<DataBean<Announcement>> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()) {
            List<Announcement> list=factory.getMapper(AnnouncementMapper.class).queryByCondition(title, beginTime, endTime, start, rows);
            Integer totalNumber=factory.getMapper(AnnouncementMapper.class).countByCondition(title, beginTime, endTime);
            rows = rows == null ?totalNumber:( rows <= 0 ? 1 : rows);Integer totalPage=totalNumber>rows?(totalNumber%rows==0?totalNumber/rows:totalNumber/rows+1):1;
            DataBean<Announcement> dataBean=new DataBean<>();
            dataBean.setList(list);
            dataBean.setPageSize(rows);
            dataBean.setTotalPage(totalPage);
            dataBean.setTotalNumber(totalNumber);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription("获取通知公告列表成功");
            msg.setData(dataBean);
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }
}
